import 'package:flutter/foundation.dart';
import 'package:get_storage/get_storage.dart';
import 'package:share_fit/app/helper/storage_helper.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';

mixin StorageMixin {
  late GetStorage _storage;
  GetStorage? _deviceStorage;

  final List<String> _storageKeys = [];
  final List<String> _deviceStorageKeys = [];

  final List<ValueSetter> _storageListeners = [];
  final List<ValueSetter> _deviceStorageListeners = [];

  void initStorage() {
    _storage = StorageHelper.getStorage(StorageHelper.storageGlobal);
    /// 设备发生变化，重新装载storage
    _storage.listenKey(UTEPedConst.keyAddress, (value) {
      _initDeviceStorage();
    });

    _initDeviceStorage();
  }

  void _initDeviceStorage() {
    var path = getStoragePath();
    var prefix = UTEPedDevice.instance.getStorePrefix();
    GetStorage tempStorage;
    if (path.isNotEmpty && prefix.isNotEmpty) {
      tempStorage = StorageHelper.getStorage(prefix + path);
    }
    else {
      tempStorage = StorageHelper.storage;
    }

    if (_deviceStorage != null && tempStorage != _deviceStorage) {
      _removeDeviceChanges();
      _deviceStorage = tempStorage;
      _restoreDeviceChanges();
    }
    else {
      _deviceStorage = tempStorage;
    }
  }

  GetStorage _getStorage(bool global) {
    if (global) {
      return _storage;
    }
    else {
      return _deviceStorage!;
    }
  }

  void save(String key, dynamic value, {global=false}) {
    /// GetStorage 保存数据
    /// [key] key
    /// [global] true表示全局数据， false表示与设备相关
    _getStorage(global).write(key, value);
    _getStorage(global).save();
  }

  dynamic read(String key, {dynamic defaultValue="", global=false}) {
    /// GetStorage 读取数据
    /// [defaultValue] 默认值
    var value = defaultValue;
    var tempStorage = _getStorage(global);
    if (tempStorage.hasData(key)) {
      value = tempStorage.read(key);
    }
    return value;
  }

  void remove(String key,  {global=false}) {
    /// GetStorage 删除数据
    /// [key] key
    /// [global] true表示全局数据， false表示与设备相关
    if (_getStorage(global).hasData(key)) {
      _getStorage(global).remove(key);
    }
  }

  /// GetStorage 获取全部key
  /// [global] true表示全局数据， false表示与设备相关
  List<dynamic> getKeys({global=false}) {
    return _getStorage(global).getKeys().toList();
  }

  void addStorageListener(String key,ValueSetter callback, {global=false}) {
    if (global) {
      _storage.listenKey(key, callback);
      if (_storageKeys.contains(key)) {
        _storageKeys.add(key);
        _storageListeners.add(callback);
      }
    }
    else {
      _deviceStorage?.listenKey(key, callback);
      if (_deviceStorageKeys.contains(key)) {
        _deviceStorageKeys.add(key);
        _deviceStorageListeners.add(callback);
      }
    }
  }

  void _removeGlobalChanges() {
    if (_storageKeys.isNotEmpty) {
      for (var change in _storageKeys) {
        _storage.changes.remove(change);
      }
    }
  }

  void _removeDeviceChanges() {
    if (_deviceStorageKeys.isNotEmpty) {
      for (var change in _deviceStorageKeys) {
        _deviceStorage?.changes.remove(change);
      }
    }
  }

  void _restoreDeviceChanges() {
    if (_deviceStorageKeys.isNotEmpty) {
      for (int i = 0;i < _deviceStorageKeys.length;i++) {
        _deviceStorage?.listenKey(_deviceStorageKeys[i], _deviceStorageListeners[i]);
      }
    }
  }

  void _removeStorageChanges() {
    _removeDeviceChanges();
    _removeGlobalChanges();
  }

  void _clearStorageListeners() {
    _deviceStorageKeys.clear();
    _deviceStorageListeners.clear();
    _storageKeys.clear();
    _storageListeners.clear();
  }

  void removeStorageListeners() {
    _removeStorageChanges();
    _clearStorageListeners();
  }

  /// 存储路径标识
  String getStoragePath() {
    return "";
  }
}